package br.cm.informatos.threads;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	private Handler handler;
	private ProgressBar progress;
	private Button startButton;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		progress = (ProgressBar) findViewById(R.id.progress_bar);
		startButton = (Button) findViewById(R.id.start_button);
		handler = new Handler();

		startButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Runnable runnable = new Runnable() {					
					@Override
					public void run() {
						for (int i = 1; i <= 10; i++){
							final int value = i;
							try{
								Thread.sleep(1000);
							}catch(InterruptedException e){
								e.printStackTrace();
							}
							handler.post(new Runnable() {
								//runOnUiThread(new  Runnable() {						

								@Override
								public void run() {
									progress.setProgress(value);

								}
							});
						}

					}
				};

				new Thread(runnable).start();

			}
		});



	}


}
